package controller.validator;

import java.util.ArrayList;
import java.util.Date;
import java.util.NoSuchElementException;

import util.ValidationUtil;
import entity.Course;
import entity.Section;
import exception.InputValidationErrors;

/**
 * Implements validation for adding, updating or deleting section.
 * 
 * @author huangjm.2012, jaslin.wee.2012
 * 
 */
public class SectionValidator extends Validator {

	private String courseCode;
	private String sectionCode;
	private String dayOfWeekStr;
	private String startTime;
	private String endTime;
	private String instructor;
	private String venue;
	private String sizeStr;

	private Section section;

	public Section getSection() {
		return section;
	}

	public SectionValidator(String courseCode, String sectionCode,
			String dayOfWeekStr, String startTime, String endTime,
			String instructor, String venue, String sizeStr) {
		super();
		this.courseCode = courseCode;
		this.sectionCode = sectionCode;
		this.dayOfWeekStr = dayOfWeekStr;
		this.startTime = startTime;
		this.endTime = endTime;
		this.instructor = instructor;
		this.venue = venue;
		this.sizeStr = sizeStr;
	}

	@Override
	public void performValidationBeforeAdding() throws InputValidationErrors {
		/*
		 * Array list to store logic error messages
		 */
		ArrayList<String> logicErrors = new ArrayList<String>();

		/*
		 * Validation for invalid course & invalid section
		 */
		Course course = null;
		try {
			course = checkForCourse(courseCode);
			validateSectionCode(sectionCode);

		} catch (NoSuchElementException e) {
			logicErrors.add(Validator.INVALID_COURSE);
		} catch (IllegalArgumentException e) {
			logicErrors.add(Validator.INVALID_SECTION);
		} catch (Exception e) {
			// should have been caught by the previous two catch clause
		}

		/*
		 * Validation for invalid day
		 */
		int dayOfWeek = 0;
		try {
			dayOfWeek = validateSectionDay(dayOfWeekStr);
		} catch (Exception e) {
			logicErrors.add(Validator.INVALID_DAY);
		}

		/*
		 * Validation for invalid start
		 */
		Date start = null;
		try {
			start = checkForInputTimeFormat(startTime);
			checkForValidSectionStartTime(startTime);
		} catch (Exception e) {
			logicErrors.add(Validator.INVALID_START);
		}

		/*
		 * Validation for invalid end
		 */
		Date end = null;
		try {
			end = checkForInputTimeFormat(endTime);
		} catch (Exception e) {
			logicErrors.add(Validator.INVALID_END);
		}

		if (start != null && end != null) {
			try {
				ValidationUtil.checkForDateSequence(start, end);
			} catch (Exception e) {
				logicErrors.add(Validator.INVALID_END);
			}
		}

		/*
		 * Validation for invalid size
		 */
		int size = 0;
		try {
			size = validateSectionSize(sizeStr);
		} catch (Exception e) {
			logicErrors.add(Validator.INVALID_SIZE);
		}

		/*
		 * Throw exception or create entity
		 */
		if (!logicErrors.isEmpty()) {
			throw new InputValidationErrors(logicErrors);
		} else {
			section = new Section(course, sectionCode, dayOfWeek, start, end,
					instructor, venue, size);
		}
	}

	@Override
	public void performValidationBeforeUpdating() throws InputValidationErrors {
		// TODO Auto-generated method stub

	}

	@Override
	public void performValidationBeforeDeleting() throws InputValidationErrors {
		// TODO Auto-generated method stub

	}

}
